package me.panpf.sketch.sample.util;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.sample.AppConfig;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.app.Context;

/**
 * 滚动中暂停暂停加载新图片管理器支持RecyclerView和AbsListView
 */
public class ScrollingPauseLoadManager implements ListContainer.ScrollListener, Component.ScrolledListener {
    private Sketch mSketch;
    private BaseItemProvider mItemProvider;

    public ScrollingPauseLoadManager(Context context, BaseItemProvider mItemProvider) {
        this.mSketch = Sketch.with(context);
        this.mItemProvider = mItemProvider;
    }

    @Override
    public void onScrollFinished() {

    }

    @Override
    public void onContentScrolled(Component component, int i0, int i1, int i2, int i3) {

    }

    @Override
    public void scrolledStageUpdate(Component component, int newStage) {
        if (AppConfig.getBoolean(component.getContext(), AppConfig.Key.SCROLLING_PAUSE_LOAD)) {
            if (newStage == Component.SCROLL_NORMAL_STAGE) {
                if (!mSketch.getConfiguration().isPauseLoadEnabled()) {
                    mSketch.getConfiguration().setPauseLoadEnabled(true);
                }
            } else if (newStage == Component.SCROLL_IDLE_STAGE) {
                if (mSketch.getConfiguration().isPauseLoadEnabled()) {
                    mSketch.getConfiguration().setPauseLoadEnabled(false);

                    if (mItemProvider != null) {
                        mItemProvider.notifyDataChanged();
                    }
                }
            }
        }
    }
}
